package com.boco.blch.runner.util;

import java.util.UUID;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.boco.blch.runner.result.Result;
import com.boco.blch.runner.result.ResultInfo;

import net.sf.json.JSONSerializer;

public class ObjectUtil {
	private static final String XML_HEAD = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
	
	private static final String XML_SPECIAL_CHAR_AND = "&";
	private static final String XML_EXCHANGE_AND = "&amp;";
	
	private static final String XML_SPECIAL_CHAR_SQ = "'";
	private static final String XML_EXCHANGE_SQ = "&apos;";
	
	private static final String XML_SPECIAL_CHAR_QUOT = "\"";
	private static final String XML_EXCHANGE_QUOT = "&quot;";
	
	private static final String XML_SPECIAL_CHAR_GT = ">";
	private static final String XML_EXCHANGE_GT = "&gt;";
	
	private static final String XML_SPECIAL_CHAR_LT = "<";
	private static final String XML_EXCHANGE_LT = "&lt;";
	
	
	public static String filenameGenerator(String suffix){
		if(suffix == null){
			return getUUID();
		} else {
			if(suffix.startsWith(".")){
				return getUUID() + suffix;
			} else {
				return getUUID() + "." + suffix;
			}
		}
	}
	
	public static String object2xml(Result re){
		 Document doc = DocumentHelper.createDocument();
		 Element response = DocumentHelper.createElement("Response"); 
		 Element result = DocumentHelper.createElement("Result"); 
		 Element errorCode  = DocumentHelper.createElement("ErrorCode"); 
		 Element errorMessage  = DocumentHelper.createElement("ErrorMessage"); 
		 Element opDetail = DocumentHelper.createElement("opDetail"); 
		 Element commandResult  = DocumentHelper.createElement("CommandResult"); 
		 response.add(result);
		 response.add(errorCode);
		 response.add(errorMessage);
		 if(re.isSuccess()){
			 response.add(opDetail);
			 opDetail.add(commandResult);
			 commandResult.setText(re.getMessage());
			 result.setText("Success");
		 } else {
			 result.setText("Error");
			 errorCode.setText(re.getCode() + "");
			 errorMessage.setText(re.getMessage());
		 }
		 doc.add(response);
		 return doc.asXML();
	}
	
	public static String obj2xml(Result result) {
		StringBuilder xmlStr = new StringBuilder(XML_HEAD);
		boolean status = result.isSuccess();
		String message = result.getMessage();
		int code = result.getCode();
		message = message.replace(XML_SPECIAL_CHAR_AND, XML_EXCHANGE_AND)
				.replace(XML_SPECIAL_CHAR_SQ, XML_EXCHANGE_SQ)
				.replace(XML_SPECIAL_CHAR_QUOT, XML_EXCHANGE_QUOT)
				.replace(XML_SPECIAL_CHAR_GT, XML_EXCHANGE_GT)
				.replace(XML_SPECIAL_CHAR_LT, XML_EXCHANGE_LT);
		if(status){
			xmlStr.append("<Response>").append("<Result>Success</Result>")
				  .append("<ErrorCode></ErrorCode><ErrorMessage></ErrorMessage><opDetail>")
				  .append("<CommandResult>").append(message).append("</CommandResult>")
				  .append("</opDetail>").append("</Response>");
		} else {
			xmlStr.append("<Response>").append("<Result>Error</Result>")
			      .append("<ErrorCode>").append(code).append("</ErrorCode>")
			      .append("<ErrorMessage>").append(message).append("</ErrorMessage>")
			      .append("</Response>");
		}
		return xmlStr.toString();
	}
	
	public static String object2json(Object obj) {
		return JSONSerializer.toJSON(obj).toString();
	}
	
	public static String getUUID(){
		UUID uuid = UUID.randomUUID();
		return uuid.toString().replace("-", "");
	}
	
	

}
